<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>系部管理</title>
    <script type="text/javascript" th:src="@{/static/layui/layui.js}"></script>
    <link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/static/css/addnews.css}"/>


</head>
<style>
    .layui-card {
        box-shadow: #c0c0c0 0 1px 3px;
    }
    .layui-row .layui-form-item {
        padding: 0 5px;
    }
</style>
<body>
<form class="layui-form layui-form-pane" style="padding: 10px;">

    <div class="layui-row">
        <div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
            <fieldset class="layui-elem-field">
                <legend>精确查询</legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <label class="layui-form-label">系部名称</label>
                        <div class="layui-input-block">
                            <select id="selByDpm" name="selByDpm" lay-filter="selByDpm" lay-verify="selByDpm"
                                    lay-search></select>
                        </div>
                    </div>
                </div>
            </fieldset>

        </div>


    </div>


</form>

<table id="evlist" lay-filter="dpmTable"></table>

<div id="editDiv" style="display: none">

    <form class="layui-form layui-form-pane" style="padding: 10px;">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label" style="color: red;">请确保名称是全称。</label>
            <div class="layui-input-block">
                <textarea id="editDpmText" placeholder="请输入系部名称" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>

</div>

<div id="addDiv" style="display: none">

    <form class="layui-form layui-form-pane" style="padding: 10px;">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label" style="color: red;">请确保名称是全称。</label>
            <div class="layui-input-block">
                <textarea id="addDpmText" placeholder="请输入系部名称" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>

</div>

</body>

</html>
<script type="text/html" id="barBtn">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
</script>

<script>
    layui.use(['jquery', 'layer', 'form', 'table', 'laydate'], function () {
        var $ = layui.$ //重点处
            , layer = layui.layer
            , form = layui.form
            , table = layui.table
            , layer = layui.layer
            , laydate = layui.laydate;


        var tableId;
        //获取分页数据列表
        tableId = table.render({
            elem: '#evlist'
            ,
            url: '/schoolmanage/findalldpm'
            ,
            cols: [
                [
                    {field: 'did', title: '系部编号', width: 200}
                    , {field: 'dname', title: '系部名称'}
                    , {field: 'dcount', title: '系部人数'}
                    , {fixed: 'right', title: '操作', toolbar: '#barBtn',width:100}
                ]
            ]
            ,
            page: true
            ,
            even: true
            ,
            toolbar: '<div><button type="button" lay-event="addDpmBtn" class="layui-btn layui-btn-sm layui-btn-normal">添加系部</button></div>'
        });
        //头工具栏事件
        table.on('toolbar(dpmTable)', function (obj) {
            if (obj.event == 'addDpmBtn') {
                $("#addDpmText").val("");
                //弹出添加问题窗口
                layer.open({
                    type: 1,
                    resize: false,
                    area: ['600px', '275px'],
                    title: '添加系部',
                    content: $('#addDiv'),
                    btn: ['确认添加', '取消'],
                    yes: function (index) {
                        var addDname = $("#addDpmText").val();
                        if (addDname.trim() == "") {
                            layer.alert('请输入系部名称！');
                            $("#addDpmText").val("");
                        } else {
                            layer.confirm('确定要添加吗？', function (index) {
                                $.ajax({
                                    type: 'post',
                                    url: '/schoolmanage/addonedpm',
                                    data: {
                                        dname: addDname
                                    },
                                    success: function (adddata) {
                                        if (adddata == true) {
                                            layer.open({
                                                content: '添加成功！',
                                                yes: function (index) {
                                                    layer.close(index);
                                                    window.location.reload();
                                                }
                                            });
                                        } else {
                                            layer.msg('添加失败，请检查是否重复或稍后重试！');
                                        }
                                    }
                                });
                                layer.close(index);
                            });
                        }


                    }
                });
            }
        });
        //监听工具条
        table.on('tool(dpmTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $("#editDpmText").val(data.dname);
                //编辑问题
                layer.open({
                    type: 1,
                    resize: false,
                    area: ['600px', '275px'],
                    title: '编辑系部名称 系部编号：' + data.did,
                    content: $('#editDiv'),
                    btn: ['提交修改', '取消'],
                    yes: function (index) {
                        var editDname = $("#editDpmText").val();
                        if (editDname.trim() == "") {
                            layer.alert('请输入系部名称！');
                            $("#editDpmText").val("");
                        } else if (editDname.trim() == data.dname.trim()) {
                            layer.alert('未做任何修改！');
                        } else {
                            layer.confirm('确定要修改吗？', function (index) {
                                $.ajax({
                                    type: 'post',
                                    url: '/schoolmanage/editonedpm',
                                    data: {
                                        did: data.did,
                                        dname: editDname
                                    },
                                    success: function (editdata) {
                                        if (editdata == true) {
                                            layer.open({
                                                content: '修改成功！',
                                                yes: function (index) {
                                                    layer.close(index);
                                                    window.location.reload();
                                                }
                                            });
                                        } else {
                                            layer.msg('修改失败，请稍后重试！');
                                        }
                                    }
                                });
                                layer.close(index);
                            });
                        }


                    }
                });
            }

        });


        /***************************************************************条件查询*********************************************************************/

        /**
         * 默认填充
         */
        //填充系部
        $.ajax({
            type: 'post',
            url: '/schoolmanage/tjseldpm?page=1&limit=1000',
            data: {},
            success: function (alldata) {

                var result = alldata.data;
                var lists = "<option value='all'>全部</option>";
                for (var i in result) {
                    lists += "<option value='" + result[i].dname + "'>" + result[i].dname + "</option>";
                }
                $('#selByDpm').html(lists);
                form.render();
            }
        });
        //按系名查询
        form.on('select(selByDpm)', function (seldata) {
            if(seldata.value=='all'){
                tableId.reload({
                    url : '/schoolmanage/findalldpm'
                });
            }else{
                tableId.reload({
                    url : '/schoolmanage/findalldpm?dname=' + seldata.value
                });
            }
            form.render();
        });

    });


</script>